package br.usp.ime.dojo.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class DojoOnline implements EntryPoint, IdentificationListener, JoinARoomListener, ExitARoomListener {
	private Identification identify;
	private VerticalPanel vPanel;
	private DojoRoomCreator roomCreator;
	private RoomListing roomListing;
	private RoomDisplay roomDisplay;
	private SessionDTO session;
	private UserDTO user;
	private IdentificationServiceAsync identificationService;

	//TODO create a component to hold this data
	//private DojoRoomDTO dojoRoomDTO;
	//private UserDTO userDTO;
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		String sessionId = Cookies.getCookie("cookie");
		
		if(sessionId == null){
			displayIdentificationBox();
		}
		else{
			identificationService = GWT.create(IdentificationService.class);
			
			AsyncCallback<SessionDTO> callback = new AsyncCallback<SessionDTO>() {
				public void onFailure(Throwable caught) {
				}

				public void onSuccess(SessionDTO result) {
					session = result;
					if(session == null){
						displayIdentificationBox();
					}
					else if(session.getUserPlace().equals("DojoRoomCreator")){
						
						AsyncCallback<UserDTO> callback = new AsyncCallback<UserDTO>() {
							public void onFailure(Throwable caught) {
							}

							public void onSuccess(UserDTO result) {
								UserDTO loggedUser = result;
								displayDojoRoomCreator(loggedUser);
							}
						};
						// Make the call to the identification service.
						identificationService.getUser(session.getUserName(), callback);
					}
					else if(session.getUserPlace().equals("RoomDisplay")){
						//TODO
					}
				}
			};
			// Make the call to the identification service.
			identificationService.getSession(sessionId, callback);
		}
		
	}


	private void displayIdentificationBox() {
		identify = new Identification();
		identify.setIdentificationListener(this);
		
		setInterface();
		
		vPanel.add(identify);

		RootPanel.get("dojoMain").setWidth("90%");
		RootPanel.get("dojoMain").add(vPanel);
	}


	public void setInterface() {
		vPanel = new VerticalPanel();
		vPanel.setWidth("100%");
		vPanel.setHeight("100%");
		vPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		vPanel.setHeight("100%");


	}

	public void onExitDojoRoom(){
		vPanel.remove(roomDisplay);
		//roomDisplay = null;
		vPanel.add(roomListing);
		vPanel.add(roomCreator);
	}

	public void onIdentificationSuccess(UserDTO loggedUser) {
		
		user = loggedUser;
		vPanel.remove(identify);
		
		Cookies.setCookie("cookie", "1");
		if(identificationService == null)
			identificationService = GWT.create(IdentificationService.class);
		
		AsyncCallback<Void> callback = new AsyncCallback<Void>(){
			public void onFailure(Throwable caught) {
			}
			public void onSuccess(Void result) {
				displayDojoRoomCreator(user);
			}
		};
		
		SessionDTO sessionDTO = new SessionDTO(loggedUser.getName(),"DojoRoomCreator","1");
		identificationService.addSession(sessionDTO, callback);
		
	}

	public void onJoinRequest(DojoRoomDTO room) {
		
		vPanel.remove(roomListing);
		vPanel.remove(roomCreator);
		roomDisplay = new RoomDisplay(room, identify.getLoggedUser());
		roomDisplay.setExitADojoRoomListener(this);
		vPanel.add(roomDisplay);
	}


	private void displayDojoRoomCreator(UserDTO loggedUser) {
		if(vPanel == null){
			setInterface();
			RootPanel.get("dojoMain").setWidth("90%");
			RootPanel.get("dojoMain").add(vPanel);
		}
		roomCreator = new DojoRoomCreator(loggedUser.getName());
		roomListing = new RoomListing();
		roomListing.setJoinRequestListener(this);
		roomCreator.setJoinRequestListener(this);
		vPanel.add(roomListing);
		vPanel.add(roomCreator);
	}
}
